﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.BeamNG.VehiclePartConfig;
using JBeamLibrary;
using JBeamLibrary.VehicleParts.Elements;

namespace DriveBeamEditor.Controls.BeamNG.SectionControls
{
    public partial class TiesControl : VehiclePartControl
    {
        #region CTORs

        public TiesControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        #region Binding source

        private void tiesBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            if (DesignMode || VehiclePart == null)
            {
                e.NewObject = new Tie();
                return;
            }

            /* Avoid loop */
            if (e.NewObject is Tie) return;

            /* Not enough nodes, do not allow */
            if (VehiclePart.NodeSection.Count < 1)
            {
                MessageBox.Show(this,
                    @"The selected vehicle part doesn't have enough nodes to create a tie. It requires a minimum of 1 node.",
                    @"Not enough nodes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tiesBindingSource.CancelEdit();
                grid.NotifyCurrentCellDirty(true);
                grid.CancelEdit();
                grid.NotifyCurrentCellDirty(false);
                return;
            }

            /* Create a new hydro */
            e.NewObject = new Tie(VehiclePart.NodeSection[0].Id, 0f, 0f, 0f, 0f, string.Empty, 0);
        }

        private void tiesBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        #endregion

        #region Data grid view

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            /* Clicks on headers */
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;

            grid_CellContentClick(sender, e);
        }

        private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            /* Clicks on headers */
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;

            /* Get column & row & cell */
            var col = grid.Columns[e.ColumnIndex];
            var row = grid.Rows[e.RowIndex];

            /* Validate new row */
            if (row.IsNewRow && col == columnDelete)
            {
                tiesBindingSource.EndEdit();
                grid.NotifyCurrentCellDirty(true);
                grid.EndEdit();
                grid.NotifyCurrentCellDirty(false);
            }

            /* Select a node for the current cell */
            else if (col.DataPropertyName.StartsWith("Node"))
            {
                if (Config.SelectNodesFromFromCurrentPartOnly)
                    pddNodeSelection.Show(grid, grid.PointToClient(Cursor.Position));
                else
                    pddNodeSelectionFull.Show(grid, grid.PointToClient(Cursor.Position));
            }

            /* Delete the current row */
            else if (col == columnDelete)
            {
                grid.Rows.RemoveAt(e.RowIndex);
            }
        }

        #endregion

        #region Popups

        private void pcNodeSelection_SelectedNodeChanged(object sender, Node e)
        {
            if (grid.CurrentRow == null || grid.CurrentCell == null || e == null) return;

            var tie = grid.CurrentRow.DataBoundItem as Tie;
            if (tie == null) return;

            var col = grid.Columns[grid.CurrentCell.ColumnIndex];
            if (col == null) return;

            if (col.DataPropertyName.Equals("NodeId"))
                tie.NodeId = e.Id;
        }

        #endregion

        #endregion

        #region Methods

        public override string ToString()
        {
            return "Ties";
        }

        private void BindTies()
        {
            grid.Enabled = false;
            tiesBindingSource.DataSource = null;

            if (VehiclePart != null)
                tiesBindingSource.DataSource = VehiclePart.TieSection.ElementCollection;

            grid.Refresh();
            grid.Enabled = true;
        }

        protected override void OnVehiclePartChanged()
        {
            pcNodeSelection.VehiclePart = VehiclePart;
            pcNodeSelectionFull.Vehicle = VehiclePart != null ? VehiclePart.File.Parent : null;
            BindTies();
            base.OnVehiclePartChanged();
        }

        #endregion
    }
}
